package com.example.springboottools.utils.pdf;
import com.example.springboottools.utils.GetKeyWordPosition;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy;
import com.itextpdf.text.pdf.parser.TextRenderInfo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class ItextPdfUtils {
        public static void main(String[] args) {
            String inputPath = "D:\\staples\\日常记录\\工作记录\\2024\\07\\pod盖章及缓存\\test\\source\\pod4507092398.pdf";
            String outputPath = "D:\\staples\\日常记录\\工作记录\\2024\\07\\pod盖章及缓存\\test\\target\\pod4507092398.pdf";
            String searchText = "直送日期";
            String replaceText = "直送日期：2024-08-21";
            try {
                addTextToPdf(inputPath, outputPath, "直送日期：2024-08-21", 300, 300);
            } catch (IOException e) {
                throw new RuntimeException(e);
            } catch (DocumentException e) {
                throw new RuntimeException(e);
            }
       /*     try {
                // Step 1: Read the original PDF document
                PdfReader reader = new PdfReader(inputPath);
                PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputPath));

                // Step 2: Find the positions of the text to be replaced
                List<Rectangle> positions = findTextPositions(stamper.getReader(), searchText);

                // Step 3: Remove the old text and Step 4: Add the new text
                for (Rectangle position : positions) {
                  //  removeText(stamper, position);
                    addText(stamper, position, replaceText);
                }

                // Step 5: Save the modified PDF document
                stamper.close();
                reader.close();
            } catch (IOException | DocumentException e) {
                e.printStackTrace();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }*/
        }
    private static List<Rectangle> findTextPositions(PdfReader reader, String searchText) throws Exception {
        List<Rectangle> positions = new ArrayList<>();
        int numberOfPages = reader.getNumberOfPages();

        for (int i = 1; i <= numberOfPages; i++) {
            String pageText = PdfTextExtractor.getTextFromPage(reader, i);

            if (pageText.contains(searchText)) {
                // This is a placeholder logic. You need to implement the actual logic to find the position.
                // For example, you can use a library like com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy.
                Rectangle rectangleExt = new Rectangle(100, 100, 100, 100);
                //rectangleExt.setPageNum(i);
                positions.add(rectangleExt); // Placeholder position
            }
        }

        return positions;
    }


    private static void removeText(PdfStamper stamper, Rectangle position) {
        PdfContentByte overContent = stamper.getOverContent(1);
        overContent.rectangle(position.getLeft(), position.getBottom(), position.getWidth(), position.getHeight());
        overContent.fill();
    }

    private static void addText(PdfStamper stamper, Rectangle position, String newText) {
        ColumnText.showTextAligned(stamper.getOverContent(1), Element.ALIGN_LEFT,
                new Phrase(newText), position.getLeft(), position.getBottom(), 0);
    }

    public static void addTextToPdf(String inputPdf, String outputPdf, String text, float x, float y) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(inputPdf);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputPdf));

        // 获取字体
        BaseFont baseFont = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        Font font = new Font(baseFont, 8);

        // 遍历每一页
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            PdfContentByte cb = stamper.getOverContent(i);
            cb.beginText();
            cb.setFontAndSize(font.getBaseFont(), font.getSize());
            cb.showTextAligned(PdfContentByte.ALIGN_LEFT, text, x, y, 0); // 在指定位置添加文本
            cb.endText();
        }

        stamper.close();
        reader.close();
    }
}
